﻿using System;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System.Windows.Input;

namespace WickedFlame.GenDock
{
    public delegate void FocusChangedEventHandler(object sender, FocusChangedEventArgs e);

    public class ImageButton : ContentControl
    {
        /// <summary>
        /// Event that gets executed when the focus changes
        /// </summary>
        public event FocusChangedEventHandler FocusChanged;

        Image buttonImage;

        public ImageButton()
        {
            this.SizeChanged += OnSizeChanged;

            buttonImage = new Image();
            buttonImage.Margin = new Thickness(2, 2, 2, 2);
            //this.Children.Add(_buttonImage);
            this.Content = buttonImage;
        }

        protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
        {
            if (IsSelected || MouseOver)
                drawingContext.DrawRectangle(
                    new LinearGradientBrush(Colors.WhiteSmoke, Colors.LightBlue, 90),
                    new Pen(Brushes.Blue, 0.5),
                    new Rect(0, 0, this.Width, this.Height)
                    );
            else
                drawingContext.DrawRectangle(
                    new LinearGradientBrush(Colors.WhiteSmoke, Colors.LightGray, 90),
                    new Pen(Brushes.Black, 0.5),
                    new Rect(0, 0, this.Width, this.Height)
                    );

            base.OnRender(drawingContext);
        }

        public ImageSource Img
        {
            get
            {
                return buttonImage.Source;
            }
            set
            {
                buttonImage.Source = value;
            }
        }

        bool _isSelected = false;
        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }
            set
            {
                _isSelected = value;
                if (this.FocusChanged != null)
                    this.FocusChanged(this, new FocusChangedEventArgs(_isSelected));
                this.InvalidateVisual();
            }
        }

        bool _mouseOver = false;
        bool MouseOver
        {
            get
            {
                return _mouseOver;
            }
            set
            {
                _mouseOver = value;
                this.InvalidateVisual();
            }
        }

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            MouseOver = true;
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            MouseOver = false;
            base.OnMouseLeave(e);
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            if (!this.IsSelected)
                this.IsSelected = true;
            else
                this.IsSelected = false;

            base.OnMouseLeftButtonUp(e);
        }

        private void OnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (buttonImage != null)
            {
                buttonImage.Width = this.Width - 4;
                buttonImage.Height = this.Height - 4;
            }
        }
    }

    public class FocusChangedEventArgs : EventArgs
    {
        bool _selected;
        public FocusChangedEventArgs(bool isSelected)
        {
            _selected = isSelected;
        }

        public bool IsSelected
        {
            get
            {
                return _selected;
            }
        }
    }
}
